package com.xiaobai.frame.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobai.common.utils.SnowFlow;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.tool.AuthUtil;
import com.xiaobai.common.utils.tool.Func;
import com.xiaobai.frame.entity.Dept;
import com.xiaobai.frame.mapper.DeptMapper;
import com.xiaobai.frame.service.IDeptService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-24
 */
@Service
@AllArgsConstructor
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    private final DeptMapper deptMapper;

    @Override
    public Result addDept(Dept dept) {
        if (Func.isEmpty(dept)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        Dept one = this.getOne(new LambdaQueryWrapper<Dept>().eq(Dept::getId, dept.getParentId()), false);
        if (Func.isNotEmpty(one)) {
            dept.setAncestors(Func.isNull(one.getAncestors()) ? String.valueOf(one.getId()) : one.getAncestors() + "," + one.getId());
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setCreateUser(AuthUtil.getId());
        dept.setId(new SnowFlow(1, 3).nextId());
        if (this.save(dept)) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "新增成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "新增失败,请联系管理人员");
    }

    @Override
    public Result updateDept(Dept dept) {
        if (Func.isEmpty(dept) || Func.isNull(dept.getId())) {
            return R.fail(ResultCode.PARAMETER_NULL, "闯入参数为null");
        }
        if (this.updateById(dept)) {
            return R.success(ResultCode.SUCCESS_NO_CONTENT, "修改成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "修改失败,请联系管理人员");
    }

    @Override
    @Transactional
    public Result deleteDept(Long[] ids) {
        if (Func.isEmpty(ids)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        if (this.removeBatchByIds(Arrays.asList(ids))) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "删除成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "删除失败,数据当中未有该对应数据");
    }

    @Override
    public Result listTreeDept(Long parentId) {
        List<Dept> deptList = deptMapper.listTreeDept(parentId);
        //构建配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        List<Tree<Long>> build = TreeUtil.build(deptList, 0L, treeNodeConfig, ((dept, treeNode) -> {
            treeNode.setId(dept.getId());
            treeNode.setParentId(dept.getParentId());
            treeNode.putExtra("deptName", dept.getDeptName());
            treeNode.putExtra("deptCategory", dept.getDeptCategory());
            treeNode.putExtra("fullName", dept.getFullName());
        }));
        return R.success(build);
    }

    @Override
    public Result getDept(Long id) {
        if (Func.isNull(id)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入数据未空");
        }
        Dept dept = this.getById(id);
        if (Func.isEmpty(dept)) {
            return R.fail(ResultCode.NOT_SEEK_RESOURCE, "数据当中未有该id数据");
        }
        return R.success(dept);
    }
}
